/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.drumkit;

import com.nokia.example.drumkit.views.Drumkit;
import com.nokia.example.drumkit.helpers.ImageLoader;
import com.nokia.example.drumkit.views.Splash;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.midlet.*;

/**
 * The main MIDlet
 */
public class Main
        extends MIDlet
        implements CommandListener {

    private static Main self;
    private Display display;
    private Drumkit view;
    private Splash splash;
    private Command exit;

    /**
     * Get a singleton instance of the Main MIDlet
     */
    public static Main getInstance() {
        return self;
    }

    /**
     * Handle application start events
     */
    public void startApp() {
        self = this;

        exit = new Command("Exit", Command.EXIT, 1);

        if (display == null) {
            display = Display.getDisplay(this);
            splash = new Splash();
            view = new Drumkit();
            display.setCurrent(splash);
        }

    }

    /**
     * Handle application pause events
     */
    public void pauseApp() {
    }

    /**
     * Handle application destroy events
     */
    public void destroyApp(boolean unconditional) {
        SoundManager.cleanUp();
    }

    /**
     * Switch to drumkit view
     */
    public void viewKit() {
        display.setCurrent(view);
    }

    /**
     * Display alert in silent mode.
     */
    public void sampleAlert() {
        Image note = ImageLoader.loadImage("/note.png");
        Alert alert = new Alert("Unable to load samples.",
                                "Sounds might not be enabled with your current Profile. Try changing "
                + "the Profile to General and restart the application..",
                                note, AlertType.WARNING);
        alert.addCommand(exit);
        alert.setCommandListener(this);
        display.setCurrent(alert, view);
    }

    /**
     * Handle command actions
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == exit) {
            exit();
        }
    }

    /**
     * Exit the application
     */
    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * @return True if the device is a S60 device
     */
    public static boolean isS60Phone() {
        String platform = System.getProperty("microedition.platform");
        if (platform == null) {
            platform = "";
        }
        if (platform.indexOf("sw_platform=S60") > 0) {
            return true;
        }
        if (platform.indexOf("/S60_") > 0) {
            return true;
        }
        try {
            Class.forName("com.symbian.gcf.NativeInputStream");
            return true;
        }
        catch (ClassNotFoundException e) {
        }

        return false;
    }
}
